package com.rd.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rd.common.CommonResult;
import com.rd.common.vo.UpdateAvatarFormData;
import com.rd.common.vo.UpdatePwdFormData;
import com.rd.mapper.SysMenuMapper;
import com.rd.mapper.SysRoleMapper;
import com.rd.mapper.SysUserMapper;
import com.rd.pojo.SysMenu;
import com.rd.pojo.SysRole;
import com.rd.pojo.SysRoleMenu;
import com.rd.pojo.SysUser;
import com.rd.service.SysUserService;
import com.rd.util.ResultUtil;
import io.netty.util.internal.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
* @author 20586
* @description 针对表【sys_user】的数据库操作Service实现
* @createDate 2023-10-12 09:25:37
*/
@Service
@Slf4j
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser>
    implements SysUserService {

    @Resource
    SysRoleMapper sysRoleMapper;
    @Resource
    SysMenuMapper sysMenuMapper;
    @Resource
    SysUserMapper sysUserMapper;


    @Override
    public SysUser getUserByName(String username) {
        //查询条件
        QueryWrapper<SysUser> queryWrapper = new QueryWrapper<>();
        //eq=equals相等
        queryWrapper.eq("username",username);
        return getOne(queryWrapper);//获取单个
    }

    @Override
    public String getAuthorityInfo(Long userId) {
        //1.创建StringBuffer
        StringBuffer buffer=new StringBuffer();
        //2.根据userId查询这个用户角色
        List<SysRole> roleList = sysRoleMapper.selectRoleByUserId(userId);
        if(Objects.nonNull(roleList) && roleList.size()>0){
            // ROLE_admin ROLE_common
            String roleStr= roleList.stream().map(role->"ROLE_" + role.getCode()).
                    collect(Collectors.joining(","));
            //把这个用户拥有的角色字符串拼接到buffer中
            buffer.append(roleStr);
        }
        //3.创建一个set集合,存储这个用户拥有的权限名称
        Set<String> menuSet=new HashSet<>();
        //4.遍历角色获取权限
        for (SysRole sysRole : roleList) {
            long roleId = sysRole.getId();
            List<SysMenu> menuList =
                    sysMenuMapper.selectMenuByRoleId(roleId);
            for (SysMenu sysMenu : menuList) {
                String perms = sysMenu.getPerms();
                if(!StringUtil.isNullOrEmpty(perms)){
                    menuSet.add(perms);
                }
            }
        }
        //把menuSet中内容拼接到buffer
        if(menuSet.size()>0){
            buffer.append(",");
            buffer.append(menuSet.stream().collect(Collectors.joining(",")));
        }
        log.info("角色权限信息:{}",buffer.toString());
        return buffer.toString();
    }

    @Override
    public void updateLastLoginTime(String username) {
        //根据用户名获取用户
        SysUser sysUser=getUserByName(username);
        UpdateWrapper<SysUser> updateWrapper=new UpdateWrapper<>();
        //修改用户信息
        updateWrapper.set("login_date",new Date());
        updateWrapper.eq("username",username);
        sysUserMapper.update(sysUser,updateWrapper);
    }



    @Override
    public CommonResult userList() {
        List<SysUser> list = sysUserMapper.selectList(null);
        return ResultUtil.buildSuccess("用户信息列表",list);
    }

    @Override
    public int updateBaseInfo(SysUser sysUser) {
        return sysUserMapper.updateBaseInfo(sysUser);
    }

    @Override
    public int updatePwd(UpdatePwdFormData pwdForm) {
        SysUser sysUser=new SysUser();
        sysUser.setId(pwdForm.getId());
        BCryptPasswordEncoder bCryptPasswordEncoder=new BCryptPasswordEncoder();
        sysUser.setPassword(bCryptPasswordEncoder.encode(pwdForm.getNewPassword()));
        sysUser.setUpdateTime(new Date());
        return  sysUserMapper.updatePwd(sysUser);
    }

    @Override
    public int updateAvatar(UpdateAvatarFormData avatarForm) {
        SysUser sysUser=new SysUser();
        sysUser.setId(avatarForm.getUserId());
        sysUser.setAvatar(avatarForm.getUserImg());
        sysUser.setUpdateTime(new Date());
        return sysUserMapper.updateAvatar(sysUser);
    }
}




